package com.cn.ysf.newbeemall.controller.admin;

import com.cn.ysf.newbeemall.common.NewBeeMallCategoryLevelEnum;
import com.cn.ysf.newbeemall.pojo.TbNewbeeMallGoodsCategory;
import com.cn.ysf.newbeemall.pojo.TbNewbeeMallGoodsInfo;
import com.cn.ysf.newbeemall.service.NewBeeMallCategoryService;
import com.cn.ysf.newbeemall.service.NewBeeMallGoodsService;
import com.cn.ysf.newbeemall.util.Result;
import com.cn.ysf.newbeemall.util.ResultGenerator;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author ysf
 * @date 2019/12/13 15:04
 */
@Controller
@RequestMapping("admin")
public class NewBeemallConfigureController {


    @Resource
    private NewBeeMallCategoryService newBeeMallCategoryService;
    @Resource
    private NewBeeMallGoodsService newBeeMallGoodsService;
    /**
     * 商品编辑页面
     * @param request
     * @return
     */
    @GetMapping("/goods/edit")
    public String toeditpage(HttpServletRequest request, HttpSession session){
        request.setAttribute("path", "edit");
        //添加
        session.setAttribute("type","add");
        //查询所有的一级分类
        List<TbNewbeeMallGoodsCategory> firstlevelcategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(0L), NewBeeMallCategoryLevelEnum.LEVEL_ONE.getLevel());
        if(!CollectionUtils.isEmpty(firstlevelcategories)){
            //获取二级分类
            List<TbNewbeeMallGoodsCategory> secondlevelcategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(firstlevelcategories.get(0).getCategoryId()),NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel());
            if(!CollectionUtils.isEmpty(secondlevelcategories)){
                //获取三级分类
                List<TbNewbeeMallGoodsCategory> thirdlevelcategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(secondlevelcategories.get(0).getCategoryId()),NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel());
                request.setAttribute("firstlevelcategories",firstlevelcategories);
                request.setAttribute("secondlevelcategories",secondlevelcategories);
                request.setAttribute("thirdlevelcategories",thirdlevelcategories);
                return "admin/goodsedit";
            }
        }
        return "admin/goodsedit";
    }


    /**
     *  点击下拉列表框动态改变三级分类信息
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/categories/listForSelect",method = RequestMethod.GET)
    @ResponseBody
    public Result listForSelect(@RequestParam("categoryId") Long categoryId){
        if(categoryId==null || categoryId<1){
            return ResultGenerator.genFailResult("缺少参数！");
        }
        //根据分类的id查询分类信息
        TbNewbeeMallGoodsCategory category=newBeeMallCategoryService.getGoodsCategoryById(categoryId);
        //如果查询到的分类信息是三级分类则不用返回数据
        if(category ==null || category.getCategoryLevel()==NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel()){
            return ResultGenerator.genFailResult("参数异常！");
        }
        Map categoryResult=new HashMap(2);
        if(category.getCategoryLevel()  ==NewBeeMallCategoryLevelEnum.LEVEL_ONE.getLevel()){
            //如果是一级分类的信息则返回当前一级分类下的所有二级分类,以及二级分类列表中的第一条数据下的三级分类
            //查询一级分类中第一条数据的二级分类
            List<TbNewbeeMallGoodsCategory> secondlevelCategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(categoryId),NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel());
            if(!CollectionUtils.isEmpty(secondlevelCategories)){
                //查询二级分类列表中第一条数据的所有三级分类
                List<TbNewbeeMallGoodsCategory> thirdlevelCategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(secondlevelCategories.get(0).getCategoryId()),NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel());
                //将查询到的二级分类信息和三级分类信息放入map集合中
                categoryResult.put("secondlevelCategories",secondlevelCategories);
                categoryResult.put("thirdlevelCategories",thirdlevelCategories);
            }
        }
        //如果是二级分类则返回三级分类列表
        if(category.getCategoryLevel()==NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel()){
            List<TbNewbeeMallGoodsCategory> thirdlevelCategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(categoryId),NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel());
            categoryResult.put("thirdlevelCategories",thirdlevelCategories);
        }
        return ResultGenerator.genSuccessResult(categoryResult);
    }


    @GetMapping("/goods/edit/{goodsId}")
    public String toeditpage(HttpServletRequest request, @PathVariable("goodsId") Long goodsId){
       /* request.setAttribute("path", "edit");*/
        //根据商品的主键id查询该商品的信息
        TbNewbeeMallGoodsInfo tbNewbeeMallGoodsInfo=newBeeMallGoodsService.selectByPrimaryKey(goodsId);
        if(tbNewbeeMallGoodsInfo==null){
            return "error/error_400";
        }
        //三级商品分类的id
        if(tbNewbeeMallGoodsInfo.getGoodsCategoryId()>0){    //如果查询的该商品的分类id大于0则说明该商品的分类级别是三级分类
            if(tbNewbeeMallGoodsInfo.getGoodsCategoryId()>0 || null !=tbNewbeeMallGoodsInfo.getGoodsCategoryId()){
                //根据商品分类id查询当前商品的分类信息
                TbNewbeeMallGoodsCategory currentGoodsCategory=newBeeMallCategoryService.getGoodsCategoryById(tbNewbeeMallGoodsInfo.getGoodsCategoryId());
                //
                if(currentGoodsCategory !=null && currentGoodsCategory.getCategoryLevel()==NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel()){
                    //查询所有的一级分类
                    List<TbNewbeeMallGoodsCategory> firstlevelcategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(0L),NewBeeMallCategoryLevelEnum.LEVEL_ONE.getLevel());
                    //根据parentId查询当前parentId下所有的三级分类
                    List<TbNewbeeMallGoodsCategory> thirdLevelCategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(currentGoodsCategory.getParentId()),NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel());
                    //查询当前三级分类的父级二级分类
                    TbNewbeeMallGoodsCategory secondcategory=newBeeMallCategoryService.getGoodsCategoryById(currentGoodsCategory.getParentId());
                    if(secondcategory!=null){
                        //根据parentId查询当前parentId下的所有二级分类
                        List<TbNewbeeMallGoodsCategory> secondLevelCategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(secondcategory.getParentId()),NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel());
                        //查询当前二级分类的父级一级分类
                        TbNewbeeMallGoodsCategory firstcategory=newBeeMallCategoryService.getGoodsCategoryById(secondcategory.getParentId());
                        if(firstcategory!=null){
                            //所有的分类数据放入reques对象中
                            request.setAttribute("firstlevelcategories",firstlevelcategories);
                            request.setAttribute("secondLevelCategories",secondLevelCategories);
                            request.setAttribute("thirdLevelCategories",thirdLevelCategories);
                            request.setAttribute("firstLevelCategoryId",firstcategory.getCategoryId());
                            request.setAttribute("secondLevelCategoryId",secondcategory.getCategoryId());
                            request.setAttribute("thirdLevelCategoryId",currentGoodsCategory.getCategoryId());
                        }
                    }
                }
            }
        }
        //如果该商品的分类id为0是
        if(tbNewbeeMallGoodsInfo.getGoodsCategoryId()==0){
            List<TbNewbeeMallGoodsCategory> firstlevelcategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(0L), NewBeeMallCategoryLevelEnum.LEVEL_ONE.getLevel());
            if(!CollectionUtils.isEmpty(firstlevelcategories)){
                //获取二级分类
                List<TbNewbeeMallGoodsCategory> secondlevelcategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(firstlevelcategories.get(0).getCategoryId()),NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel());
                if(!CollectionUtils.isEmpty(secondlevelcategories)){
                    //获取三级分类
                    List<TbNewbeeMallGoodsCategory> thirdlevelcategories=newBeeMallCategoryService.selectBylevelAndParentAndNumber(Collections.singletonList(secondlevelcategories.get(0).getCategoryId()),NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel());
                    request.setAttribute("firstlevelcategories",firstlevelcategories);
                    request.setAttribute("secondlevelcategories",secondlevelcategories);
                    request.setAttribute("thirdlevelcategories",thirdlevelcategories);
                }
            }
        }
        request.setAttribute("goods",tbNewbeeMallGoodsInfo);

        return "admin/goodsedit";
    }







    /**
     * 轮播图配置
     * @param request
     * @return
     */
    @GetMapping("/goods/carouse")
    public String tocarousel(HttpServletRequest request){

        request.setAttribute("path", "carouse");
        return "admin/carousel";
    }

    /**
     * 热销商品配置
     * @param request
     * @return
     */
    @GetMapping("/goods/hot")
    public String tohotgoods(HttpServletRequest request){

        request.setAttribute("path", "hot");
        return "admin/hotgoods";
    }
    /**
     * 新品上线配置
     * @param request
     * @return
     */
    @GetMapping("/goods/news")
    public String tonewsgoods(HttpServletRequest request){

        request.setAttribute("path", "news");
        return "admin/newgoods";
    }
    /**
     * 为您推荐商品配置
     * @param request
     * @return
     */
    @GetMapping("/goods/recommend")
    public String torecommendgoods(HttpServletRequest request){

        request.setAttribute("path", "recommend");
        return "admin/recommend";
    }
    /**
     * 商品分类管理
     * @param request
     * @return
     */
    @GetMapping("/goods/categorymanage")
    public String goodscategorymanage(HttpServletRequest request){

        request.setAttribute("path", "categorymanage");
        return "admin/categorymanage";
    }






}
